package com.sinosoft.perfect.system._authorization.dataFormat;

import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;

import com.sinosoft.perfect.system._.po.TResourceSystem;
import com.sinosoft.perfect.system._.util.XmlUtil;

public class AuthorizationTreeDataFormat_DHX implements IAuthorizationTreeDataFormat {

	@Override
	public String dataFormatRoleSystemResourceTree(List<TResourceSystem> resourceList, List<String> resourceIds) {
		Document document = XmlUtil.createDocument();
		Element rootElement = XmlUtil.createRootElement(document, "tree");
		rootElement.addAttribute("id", "0");

		if (resourceList != null && resourceList.size() != 0) {
			for (int i = 0; i < resourceList.size(); i++) {
				TResourceSystem resource = resourceList.get(i);

				if (resource.getResourceLevel() == 1) {
					Element nowNode = XmlUtil.createElement("item");
					XmlUtil.editElementAttribute(nowNode, "id", resource.getResourceId());
					XmlUtil.editElementAttribute(nowNode, "text", resource.getResourceName());
					if ("0".equals(resource.getStatus())) {
						XmlUtil.editElementAttribute(nowNode, "style", "color:red");
					}
					if (resourceIds != null && resourceIds.size() != 0) {
						for (int j = 0; j < resourceIds.size(); j++) {
							if (resource.getResourceId().equals(resourceIds.get(j))) {
								XmlUtil.editElementAttribute(nowNode, "style", "color:blue");
								XmlUtil.editElementAttribute(nowNode, "checked", "1");
							}
						}
					}
					XmlUtil.addElementChild(rootElement, nowNode);
					addRoleSystemResourceChild(resource.getResourceId(), resourceList, resourceIds, nowNode);

				}
			}
		}
		return XmlUtil.getDocumentToXmlString(document);
	}

	private <T> void addRoleSystemResourceChild(String superId, List<T> resourceList, List<String> resourceIds, Element parentNode) {
		for (int i = 0; i < resourceList.size(); i++) {
			TResourceSystem resource = (TResourceSystem) resourceList.get(i);
			Element nowNode = XmlUtil.createElement("item");
			XmlUtil.editElementAttribute(nowNode, "id", resource.getResourceId());
			XmlUtil.editElementAttribute(nowNode, "text", resource.getResourceName());
			if ("0".equals(resource.getStatus())) {
				XmlUtil.editElementAttribute(nowNode, "style", "color:red");
			}
			if (resourceIds != null && resourceIds.size() != 0) {
				for (int j = 0; j < resourceIds.size(); j++) {
					if (resource.getResourceId().equals(resourceIds.get(j))) {
						XmlUtil.editElementAttribute(nowNode, "style", "color:blue");
						XmlUtil.editElementAttribute(nowNode, "checked", "1");
					}
				}
			}
			if (superId.equals(resource.getSuperResourceId())) {
				XmlUtil.addElementChild(parentNode, nowNode);
				if (hasResourceChild(resourceList, resource.getResourceId()))
					addRoleSystemResourceChild(resource.getResourceId(), resourceList, resourceIds, nowNode);
			}
		}
	}

	@SuppressWarnings("unchecked")
	private <T> boolean hasResourceChild(List<T> resourceList, String superId) {
		boolean result = false;
		Iterator<TResourceSystem> resourceIterator = (Iterator<TResourceSystem>) resourceList.iterator();
		while (resourceIterator.hasNext()) {
			TResourceSystem resource = resourceIterator.next();
			if (superId.equals(resource.getSuperResourceId())) {
				result = true;
				break;
			}
		}
		return result;
	}

}
